/****************************************************************************/
/*This program opens the raw tornado data and cleans and prepares it        */
/*to be merged with the business data in the next program                   */
/*Keep the blocks within the 0.5-1.5 mile buffer                            */
/*raw data is noccp_tornado_sample_not_balanced_within_15_ttype3_update     */
/****************************************************************************/
/*Modified on Rohlin's code                                                 */

*#delimit;
clear
set matsize 800
set more off
capture log close
set trace off

/****************************************************************************/
/* DEFINE THE PROJECT FOLDER                                                */
/****************************************************************************/

local pathsmr C:\Research

/****************************************************************************/
/*DEFINE PATH TO FOLDERS                                                    */
/****************************************************************************/

local Torpath  `pathsmr'\Tornadoes\0515Miles_CountyM

/****************************************************************************/
/*Open tornado intensity data                                               */
/****************************************************************************/

use `Torpath'\Data\hit_blocks_intensity_sample.dta, clear

/****************************************************************************/
/*Clean tornado intensity data                                              */
/****************************************************************************/

rename id tornado_id
rename year tor_year
rename fips_num bgfips

gen treated = 1

sort bgfips

/*For tornado ID=87 & 74, correct IA data */
replace individual_assistance=0 if tornado_id==87
replace individual_assistance=1 if tornado_id==74

save `Torpath'\Data\tornado_intensity_data_cleaned.dta, replace

keep bgfips public_assistance individual_assistance tornado_id
isid tornado_id bgfips

save `Torpath'\Temp\program2_doubles.dta, replace

collapse (mean) public_assistance individual_assistance, by(tornado_id)

sort tornado_id
save `Torpath'\Temp\program2_temp.dta, replace


/****************************************************************************/
/****************************************************************************/
/*Make a 1to2 mile buffer zone as a robustness check                        */
/****************************************************************************/
/****************************************************************************/

use `Torpath'\Data\tornado_blocks_buffersF4F5.dta, clear

keep if buffer_dist == "1"

sort tornado_id fips_num
save `Torpath'\Temp\program2_1milebuff_temp.dta, replace


use `Torpath'\Data\tornado_blocks_buffersF4F5.dta, clear

keep if buffer_dist == "2"

sort tornado_id fips_num 
merge 1:1 tornado_id fips_num using  `Torpath'\Temp\program2_1milebuff_temp.dta
tab _merge
keep if _merge == 1
drop _merge

sort fips_num
save `Torpath'\Temp\program2_1to2milebuff_temp.dta, replace

/****************************************************************************/
/*Open buffer counterfactual tornado data                                   */
/****************************************************************************/

/*toggle this if we are doing the robustness check or not*/
use `Torpath'\Data\tornado_blocks_buffersF4F5.dta, clear

keep if buffer_dist == "05_15"

*use `Torpath'\Temp\program2_1to2milebuff_temp.dta, clear



/****************************************************************************/
/*Clean buffer tornado data                                                 */
/****************************************************************************/

rename fips_num bgfips

gen treated = 0

gen tor_year = substr(date,-4,4)
destring tor_year, replace

/* Only keep the control blocks that are within 2 miles buffer*/

keep if tornado_id == 3 | tornado_id == 4 | tornado_id == 12 | tornado_id == 18 | tornado_id == 24 | ///
        tornado_id == 25 | tornado_id == 26 | tornado_id == 28 | tornado_id == 36 | tornado_id == 41 |  ///
        tornado_id == 43 | tornado_id == 45 | tornado_id == 49 | tornado_id == 52 | tornado_id == 55 | tornado_id == 63 |  ///
        tornado_id == 70 | tornado_id == 74 | tornado_id == 87 | tornado_id == 88 | tornado_id == 90 |  ///
        tornado_id == 91 | tornado_id == 98 | tornado_id == 99 | tornado_id == 100 | tornado_id == 102 |  ///
        tornado_id == 104 | tornado_id == 105 | tornado_id == 106 | tornado_id == 107 | tornado_id == 108 |  ///
        tornado_id == 110 | tornado_id == 111 | tornado_id == 112 | tornado_id == 114  

isid tornado_id bgfips

replace bgfips = "0" + bgfips if length(bgfips) == 14

sort bgfips
merge m:1 bgfips using  `Torpath'\Temp\program2_doubles.dta
keep if _merge == 1   
*_merge==3 for 5272 blocks, meaning that the same block is both in the hit file and in the buffer file
*To avoid this, we only include a block as a potential control if has no area of the block hit. Dont consider matched blocks as control obs. 

drop _merge public_assistance individual_assistance

sort tornado_id
merge m:1 tornado_id using `Torpath'\Temp\program2_temp.dta //see which tornado received PA and IA
drop _merge

/****************************************************************************/
/*append intensity data to the buffer tornado data                          */
/****************************************************************************/

sort bgfips
append using `Torpath'\Data\tornado_intensity_data_cleaned.dta

replace individual_assistance=1 if tornado_id==25
replace individual_assistance=1 if tornado_id==26
replace individual_assistance=1 if tornado_id==28 & state_fips == "29"

replace public_assistance=1 if tornado_id==25
replace public_assistance=1 if tornado_id==26
replace public_assistance=1 if tornado_id==28 & state_fips == "29"

replace individual_assistance=0 if tornado_id==87
replace individual_assistance=1 if tornado_id==74

/****************************************************************************/
/*Timing adjustment                                                         */
/****************************************************************************/   

/*Add 1 yr to tornado year (t) for 7 tornadoes because there is one year lag between
 the year at which the businesses were adversely affected and the tornado year. */

/*New*/
gen tornado_year_temp = tor_year
replace tornado_year_temp = tornado_year_temp + 1 if tornado_id == 24 | tornado_id == 25  | tornado_id == 26 | tornado_id == 4 | tornado_id == 49  


/****************************************************************************/
/*Adjust the drop command to determine which sample you wish                */
/****************************************************************************/   

*Our main regressions are with 34 tornado specification is: 
*drop if tornado_id == 111 

*Selected sample based on CCP: 
*keep if tornado_id == 12 | tornado_id == 63 | tornado_id ==  74 | tornado_id == 90 | tornado_id == 98 | tornado_id == 100 | tornado_id == 105 | tornado_id == 106 | tornado_id == 112 | tornado_id == 4  | tornado_id == 28  | tornado_id == 36 | tornado_id == 41 | tornado_id == 43 | tornado_id == 49 | tornado_id == 87  | tornado_id == 88 | tornado_id == 99 | tornado_id == 110

*Dan's list of 18 tornadoes
*drop if tornado_id == 3 | tornado_id == 18 | tornado_id == 24 | tornado_id == 25 | tornado_id == 26 | tornado_id == 28 | tornado_id == 45 | tornado_id == 52 | tornado_id == 55 | tornado_id  == 70 | tornado_id == 91 | tornado_id == 102  | tornado_id == 104 | tornado_id == 107 | tornado_id == 108 | tornado_id == 114 | tornado_id == 111


/****************************************************************************/
/*save cleaned tornado data                                                 */
/****************************************************************************/

sort bgfips
save `Torpath'\Data\tornado_data_cleaned_0515MilesBuffer.dta, replace
